package com.glsc.ngateway.opmanage.controller.archive;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.archive.CounterParty;
import com.glsc.ngateway.opmanage.dto.CounterPartyExcelDto;
import com.glsc.ngateway.opmanage.dto.CounterPartyQueryDto;
import com.glsc.ngateway.opmanage.service.archive.CounterPartyService;
import com.glsc.ngateway.opmanage.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.opmanage.utils.report.ReportFileUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 交易对手方信息
 */
@RestController
@RequestMapping("/api/party")
@Slf4j
public class CounterPartyController {

    @Resource
    private CounterPartyService counterPartyService;


    /**
     * 分页查询交易对手方信息
     *
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<CounterParty>> findCounterPartyPage(@RequestBody CounterPartyQueryDto counterPartyQueryDto) {
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(counterPartyQueryDto.getPartyName())) {
            param.put("LIKE_partyName", counterPartyQueryDto.getPartyName());
        }
        if (StringUtils.isNotBlank(counterPartyQueryDto.getPartyOrganCode())) {
            param.put("LIKE_partyOrganCode", counterPartyQueryDto.getPartyOrganCode());
        }
        Page<CounterParty> page = counterPartyService.findCounterPartyPage(counterPartyQueryDto.getPageNo(), counterPartyQueryDto.getPageSize(), param);
        return PlatformResponse.successData(page);
    }

    @PostMapping("/export")
    public byte[] orgClientPageExport(@RequestBody CounterPartyQueryDto counterPartyQueryDto, HttpServletResponse response) throws IOException {

        response.setHeader("Content-Disposition", "attachment;" + "filename=" + URLEncoder.encode(ReportFileConfigEnum.COUNTER_PARTY_EXPORT.getFileName(), "UTF-8"));
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(counterPartyQueryDto.getPartyName())) {
            param.put("LIKE_partyName", counterPartyQueryDto.getPartyName());
        }
        if (StringUtils.isNotBlank(counterPartyQueryDto.getPartyOrganCode())) {
            param.put("LIKE_partyOrganCode", counterPartyQueryDto.getPartyOrganCode());
        }
        Page<CounterParty> clientPage = counterPartyService.findCounterPartyPage(PageUtil.initPageNo(1), PageUtil.initPageSize(3000), param);
        if (Objects.isNull(clientPage) || CollectionUtils.isEmpty(clientPage.getContent())) {
            return null;
        }
        List<CounterPartyExcelDto> resList = Lists.newArrayList();
        for (CounterParty info : clientPage.getContent()) {
            CounterPartyExcelDto excelDto = new CounterPartyExcelDto();
            BeanUtils.copyProperties(info, excelDto);
            if (StringUtils.isNotBlank(excelDto.getEnableFlag())) {
                if (excelDto.getEnableFlag().equals("Y")) {
                    excelDto.setEnableFlag("是");
                } else {
                    excelDto.setEnableFlag("否");
                }
            }
            resList.add(excelDto);
        }
        return ReportFileUtil.export(resList, ReportFileConfigEnum.COUNTER_PARTY_EXPORT);
    }

    /**
     * 根据id查找交易对手方信息
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public PlatformResponse<CounterParty> findById(@ApiParam(value = "交易对手方id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(counterPartyService.findById(id));
    }

    /**
     * 保存交易对手方信息
     *
     * @param counterParty
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody CounterParty counterParty) {
        if (StringUtils.isNotBlank(counterParty.getPartyName())) {
            counterParty.setPartyName(counterParty.getPartyName().trim().replace(" ", ""));
        }
        if (StringUtils.isNotBlank(counterParty.getPartyOrganCode())) {
            counterParty.setPartyOrganCode(counterParty.getPartyOrganCode().trim().replace(" ", ""));
        }
        return PlatformResponse.successData(counterPartyService.save(counterParty));
    }

    /**
     * 删除，在使用中的交易对手禁止删除
     *
     * @param counterParty
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody CounterParty counterParty) {
        counterPartyService.delete(counterParty.getId());
        return PlatformResponse.success();
    }


    /**
     * 获取所有的交易启用的交易对手方信息
     *
     * @return
     */
    @GetMapping("/list")
    public PlatformResponse<List<CounterParty>> listAllEnable() {
        return PlatformResponse.successData(counterPartyService.listAllEnablePart());
    }


}
